package mvc;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mvc.action.LoginAction;
import mvc.action.RegisterAction;
import mvc.form.Action;
import mvc.form.ActionForm;
import mvc.form.LoginForm;
import mvc.form.Register;
import mvc.unit.FullBean;

public class ActionServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		System.out.println("sign="+request.getParameter("sign"));
		request.setCharacterEncoding("GBK");
		response.setCharacterEncoding("GBK");
//		String sign =request.getParameter("sign");
//		LoginForm form = (LoginForm)FullBean.full(request);
		Action action= null;
		ActionForm form = FullBean.full(request);
//		if(request.getParameter("sign").equals("mvc.form.LoginForm")){
//			
//			action = new LoginAction();
//						
//		}
//		
//       if(request.getParameter("sign").equals("mvc.form.Register")){
//			
//			action = new RegisterAction();
//						
//		}
		Map<String,String> map = ActionMapping.getMap();
		try{
			String actionname = map.get(request.getParameter("sign"));
			Class clazz = Class.forName(actionname);
			action = (Action) clazz.newInstance();
			
		}catch(Exception e){
			
		}
		String message = action.excute(form);
		PrintWriter out = response.getWriter();
		out.print(message);
		out.flush();
		out.close();
		
		
		System.out.println(form);
//		
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}


}
